package cn.com.cetccst.location;

import static com.amap.api.fence.GeoFenceClient.GEOFENCE_IN;
import static com.amap.api.fence.GeoFenceClient.GEOFENCE_OUT;
import static com.amap.api.fence.GeoFenceClient.GEOFENCE_STAYED;

import android.content.Context;
import android.content.IntentFilter;
import android.util.Log;

import com.amap.api.fence.GeoFence;
import com.amap.api.fence.GeoFenceClient;
import com.amap.api.fence.GeoFenceListener;
import com.amap.api.location.DPoint;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeoFenceManager {
    public static final String TAG = "GeoFenceManager";

    public static final String GEOFENCE_BROADCAST_ACTION = "ss.amaploc.geofence";

    //目前客户端全局只能有一个地理围栏
    public static final String FENCE_GLOBAL = "fence_global";

    private GeoFenceListener mFenceListener;
    private GeoFenceClient mGeoFenceClient;
    private Context mContext;
    private Map<String, GeoFence> mFenceMap;
    private Map<String, Integer> mFenceStatusMap;

    public GeoFenceManager(Context context) {
        this.mFenceMap = new HashMap<>();
        this.mFenceStatusMap = new HashMap<>();
        this.mGeoFenceClient = new GeoFenceClient(context);
        this.mContext = context;
        initReceiver();

        // 初始化围栏监听器
        mFenceListener = new GeoFenceListener() {
            @Override
            public void onGeoFenceCreateFinished(List<GeoFence> list, int errorCode, String s) {
                if (errorCode == GeoFence.ADDGEOFENCE_SUCCESS) {//判断围栏是否创建成功
                    Log.i(TAG, "onGeoFenceCreateFinished success create geo fence ");
                    for (GeoFence fence : list) {
                        Log.i(TAG, "onGeoFenceCreateFinished geo fence status ： " + fence.getCustomId() + " / " + fence.getStatus());
                        mFenceMap.put(fence.getCustomId(), fence);
                        mFenceStatusMap.put(fence.getFenceId(), fence.getStatus());
                    }
                } else {
                    Log.i(TAG, "onGeoFenceCreateFinished failed create geo fence : " + errorCode);
                }
            }
        };

        mGeoFenceClient.setActivateAction(GEOFENCE_IN | GEOFENCE_OUT | GEOFENCE_STAYED);
        mGeoFenceClient.setGeoFenceListener(mFenceListener);
        mGeoFenceClient.createPendingIntent(GEOFENCE_BROADCAST_ACTION);
    }

    private void initReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(GEOFENCE_BROADCAST_ACTION);
        GenFenceBroadcastReceiver receiver = new GenFenceBroadcastReceiver();
        mContext.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED);
    }

    public int getGeoFenceStatus(String fenceName) {
        GeoFence fence = mFenceMap.get(fenceName);
        if (fence == null) {
            return GeoFence.STATUS_UNKNOWN;
        }
        return mFenceStatusMap.get(fence.getFenceId());
    }

    public void createCircleFence(String fenceName, double lat, double lon, float radius) {
        DPoint dPoint = new DPoint();
        dPoint.setLatitude(lat);
        dPoint.setLongitude(lon);
        Log.i(TAG, "create circle fence :" + fenceName + " center:" + dPoint.getLatitude() + "," + dPoint.getLongitude());
        mGeoFenceClient.removeGeoFence();
        mGeoFenceClient.addGeoFence(dPoint, radius, fenceName);
    }

    public void createGlobalFence(double lat, double lon, float radius) {
        createCircleFence(FENCE_GLOBAL, lat, lon, radius);
    }

    public void removeFences() {
        mGeoFenceClient.removeGeoFence();
    }

    protected void updateStatus(String fenceid, int status) {
        Log.i(TAG, "update fenceid:" + fenceid + " status:" + status);
        mFenceStatusMap.put(fenceid, status);
    }
}
